﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Sales_SaleSrh
 * 类型：       控制层
 * 功能说明：   销售资料检索页面
 * 作者：       黄鑫
 * 创建日期：   2007/08/22
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.App.Master.Bean;
using Shpm.App.Master.Dao;
using log4net;
using Shpm.System;
using Shpm.App.Commons.Utils;

public partial class App_Search_SaleSrhForm : System.Web.UI.Page
{
    protected static ILog log = LogManager.GetLogger(typeof(App_Search_SaleSrhForm));
    private static string SALE_DOC_TYPE = "SALE_DOC_TYPE";
    private static string SALE_DOC_TYPE_ALL = "ALL";

    // form load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            // 接收传递参数为分页编号
            if (Context.Items.Contains("ParamPageIndex"))
            {
                viewSrhRst.PageIndex = Convert.ToInt32(Context.Items["ParamPageIndex"]);
            }
            this.ViewState["docType"] = Convert.ToString(Context.Items["docType"]);
        }
        CommonDictionaryDao dao = new CommonDictionaryDao();
        CommonDictionaryBean dataBean = new CommonDictionaryBean();
        dataBean.Catalog  = SALE_DOC_TYPE;
        IList dataList = dao.FindByCommonDictionary(dataBean);
        HtmlTableRow row = new HtmlTableRow();
        tblPanel1.Rows.Add(row);
        Button btn = new Button();
        btn.ID = SALE_DOC_TYPE_ALL ;
        btn.Text = "全部显示";
        btn.Click += new EventHandler(Button1_Click);

        btn.Width = 65;
        HtmlTableCell cell = new HtmlTableCell();
        cell.Width = "80";
        cell.Controls.Add(btn);
        row.Cells.Add(cell);
        if(dataList != null )
        {
            for (int i = 0; i < dataList.Count; i++)
            {
                CommonDictionaryBean beanResult = (CommonDictionaryBean)dataList[i];
                btn = new Button();
                btn.ID = beanResult.DictNo;
                btn.Text = beanResult.Content;
                btn.Click += new EventHandler(Button1_Click);

                btn.Width = 65;
                cell = new HtmlTableCell();
                cell.Width = "80";
                cell.Controls.Add(btn);
                row.Cells.Add(cell);
            }
        }

        btn = new Button();
        btn.ID = "btnBack";
        btn.Text = "返回";
        btn.Width = 65;
        btn.Click += new EventHandler(btnBack_Click);
        cell = new HtmlTableCell();
        cell.Width = "80";
        cell.Controls.Add(btn);
        row.Cells.Add(cell);
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        MainMenuManager manager = new MainMenuManager(Context);
        MainMenuBean menuBean = manager.GetCurrentMenu();

        manager.Forward(menuBean);
    }

    // details click
    protected void viewSrhRst_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "details")
        {
            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Get the Key value from the GridView control.
            DataKey key = viewSrhRst.DataKeys[index];

            string docId = key.Value.ToString();
            //Request.Params["abc"] = "default";
            Context.Items["docId"] = docId;
            // 传递分页编号
            Context.Items["ParamPageIndex"] = viewSrhRst.PageIndex;
            Context.Items["docType"] = this.ViewState["docType"];
            // Server.Transfer("./SaleDetailsInfoForm.aspx", true);
            ControllerUtils.Transfer(this.Context, "~/App/Search/SaleDetailsInfoForm.aspx", true);
        }

    }


    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        DocContractBean dataBean = new DocContractBean();
        dataBean.Type = Convert.ToString(this.ViewState["docType"]);
        if (dataBean.Type == SALE_DOC_TYPE_ALL)
        {
            dataBean.Type = null;
        }
        DocContractDao dao = new DocContractDao();
        IList dataList = dao.ListDocContract(dataBean);
        viewSrhRst.DataSource = dataList;
        if (dataList.Count == 0)
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0005").ToString();
            tblResult.Visible = false;
        }
        else
        {
            tblResult.Visible = true;
        }
        viewSrhRst.DataBind();
    }

    // page index change
    protected void viewSrhRst_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        // 设置分页编号
        viewSrhRst.PageIndex = e.NewPageIndex;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        this.ViewState["docType"] = btn.ID;
    }
}
